package com.quasar.hpatient.bean.home_bloodpressure;

import android.text.TextUtils;

import com.quasar.hpatient.R;

import lib.quasar.context.BaseConstant;
import lib.quasar.util.LogUtil;
import lib.quasar.widget.chart.entry.Entry;

/**
 * description: 首页 ==> 血压
 * created by kalu on 2018/3/29 9:03
 */
public class BloodpressureItemBean implements Entry {

    private String record_datetime = "";
    private String record_dose = "";
    private String record_type = "0";
    // 高压
    private String record_systolic = "";
    // 低压
    private String record_diastolic = "";

    public String getRecord_datetime() {
        return record_datetime;
    }

    public void setRecord_datetime(String record_datetime) {
        this.record_datetime = record_datetime;
    }

    public String getRecord_dose() {
        return record_dose;
    }

    public void setRecord_dose(String record_dose) {
        this.record_dose = record_dose;
    }

    public String getRecord_type() {
        return record_type;
    }

    public void setRecord_type(String record_type) {
        this.record_type = record_type;
    }

    public String getRecord_systolic() {
        return record_systolic;
    }

    public void setRecord_systolic(String record_systolic) {
        this.record_systolic = record_systolic;
    }

    public String getRecord_diastolic() {
        return record_diastolic;
    }

    public void setRecord_diastolic(String record_diastolic) {
        this.record_diastolic = record_diastolic;
    }

    @Override
    public String getAxisX() {
        return getLogtime(true);
    }

    @Override
    public float getAxisYMax() {
        return 0;
    }

    @Override
    public float getAxisYMin() {
        return 0;
    }

    public String getLogtime(boolean onlyHour) {

        if (!TextUtils.isEmpty(record_datetime) && record_datetime.length() != 18) {
            return record_datetime.substring(onlyHour ? 11 : 0, 16);
        } else {
            return "时间错误";
        }
    }

    @Override
    public String getUnit() {
        return BaseConstant.NULL_STR;
    }

    @Override
    public float getValue() {
        return 0f;
    }

    @Override
    public float[] getValues() {
        final float[] temp = new float[2];

        try {
            temp[0] = Float.parseFloat(record_systolic);
        } catch (Exception e) {
            temp[0] = 0f;
            LogUtil.e("", e.getMessage(), e);
        }

        try {
            temp[1] = Float.parseFloat(record_diastolic);
        } catch (Exception e) {
            temp[1] = 0f;
            LogUtil.e("", e.getMessage(), e);
        }
        return temp;
    }

    public int getColor() {
        try {
            float high = Float.parseFloat(record_systolic);
            float low = Float.parseFloat(record_diastolic);
            return (high <= 120f && high >= 80.1f && low <= 80f && low >= 40.1f) ? R.color.color_bg_theme : R.color.color_bg_peach;
        } catch (Exception e) {
            e.printStackTrace();
            return R.color.color_bg_theme;
        }
    }

    public int getBloodpressureColor() {
        try {
            float high = Float.parseFloat(record_systolic);
            float low = Float.parseFloat(record_diastolic);
            return (high <= 120f && high >= 80.1f && low <= 80f && low >= 40.1f) ? R.color.color_black : R.color.color_bg_peach;
        } catch (Exception e) {
            e.printStackTrace();
            return R.color.color_bg_peach;
        }
    }

    public String getState() {
        try {

            String returnValue = "";

            float high = Float.parseFloat(record_systolic);
            float low = Float.parseFloat(record_diastolic);
            // 低血压：收缩压40.1—80；舒张压30.1—40
            if ((high <= 80f) || (low <= 40f)) {
                returnValue = "低血压";
            }
            // 2.正常：收缩压80.1—120；舒张压40.1—80
            if ((high <= 120f && high >= 80.1f) && (low <= 80f && low >= 40.1f)) {
                returnValue = "正常";
            }
            // 3.正常高值：收缩压120.1—139；舒张压80.1—89
            if ((high <= 139f && high >= 120.1f) || (low <= 89f && low >= 80.1f)) {
                returnValue = "正常高值";
            }
            // 4.1级高血压（轻度）：收缩压139.1—159；舒张压89.1—99
            if ((high <= 159f && high >= 139.1f) || (low <= 99f && low >= 89.1f)) {
                returnValue = "1级高血压(轻度)";
            }
            // 5.2级高血压（中度）：收缩压159.1—179；舒张压99.1—109
            if ((high <= 179f && high >= 159.1f) || (low <= 109f && low >= 99.1f)) {
                returnValue = "2级高血压(中度)";
            }
            // 6.3级高血压（重度）：收缩压≥179.1；舒张压≥109.1
            if ((high >= 179.1f) || (low >= 109.1f)) {
                returnValue = "3级高血压(重度)";
            }
            // 7.单纯收缩期高血压：收缩压≥140；舒张压＜90
            if ((high >= 140f && low <= 90f)) {
                returnValue = "单纯收缩期高血压";
            }
            if ("".equals(returnValue)) {
                returnValue = "异常信息";
            }
            return returnValue;

        } catch (Exception e) {
            e.printStackTrace();
            return "异常信息";
        }
    }

    @Override
    public String getDate() {
        try {
            final int length = record_datetime.length();
            return record_datetime.substring(length - 8, length - 3);
        } catch (Exception e) {
            LogUtil.e("", e.getMessage(), e);
            return "未知时间";
        }
    }

    @Override
    public String getName() {
        return null;
    }

    public String getInput() {
        if (TextUtils.isEmpty(record_systolic) && TextUtils.isEmpty(record_diastolic)) {
            return "-/-";
        } else if (TextUtils.isEmpty(record_diastolic)) {
            return "-/" + record_diastolic;
        } else if (TextUtils.isEmpty(record_systolic)) {
            return record_systolic + "/-";
        } else {
            return record_systolic + "/" + record_diastolic;
        }
    }

    public String getKey() {
        if (!TextUtils.isEmpty(record_datetime) && record_datetime.length() != 18) {
            return record_datetime.substring(0, 11) + "00:00:00";
        } else {
            return "0000-00-00 00:00:00";
        }
    }

    public boolean isWarning() {
        return !getState().startsWith("正常");
    }

    public boolean isLessTime(String datetime) {

        /**
         * 如果参数字符串等于此字符串，则返回值 0；
         * 如果此字符串小于字符串参数，则返回一个小于 0 的值；
         * 如果此字符串大于字符串参数，则返回一个大于 0 的值。
         */
        return record_datetime.compareToIgnoreCase(datetime) < 0;
    }
}
